package com.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.user.common.R;
import com.user.config.AliyunOSSUtils;
import com.user.entity.ResultResponse;
import com.user.service.IUserService;
import com.user.service.RecordService;
import com.user.vo.BirthdayVo;
import com.user.vo.RecordVo;
import com.user.vo.UserVo;
import com.usian.model.record.Recordd;
import com.usian.model.shop.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 登录 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-17
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @Autowired
    private RecordService recordService;

    @RequestMapping("/login")
    public R login(String phone, Integer verify) {
        return R.success(userService.login(phone, verify));
    }

    @PostMapping("/save")
    public R save(@RequestBody User user) {
        return R.success(userService.insert(user));
    }

    @RequestMapping("/findList")
    public R findList(User user) {
        List<User> list = userService.findList(user);
        return R.success(list);
    }
    //根据id查询用户
    @PostMapping("findById")
    public R findById(Integer id) {
        // 根据id查询用户信息
        User user = userService.findById(id);
        System.out.println(user);
        return R.success(user);
    }
    //根据id修改昵称
    @PostMapping("updateById")
    public void updateById(String name, Integer id) {
        System.out.println(name);
        System.out.println(id);
        userService.updateUserById(name, id);
    }
    //设置交易密码
    @RequestMapping("/setPayPassword")
    public ResultResponse setPayPassword(Integer uid, Integer code) {
        System.out.println("++++" + uid + code);
        return userService.setPayPassword(uid, code);
    }

    @RequestMapping("getPayPassword")
    public ResultResponse getPayPassword(Integer id) {
        return userService.getPayPassword(id);
    }


    //根据id查询账户积分
    @RequestMapping("findScoreId")
    public Integer findScoreId(@RequestBody Integer id) {
        return userService.findScoreId(id);
    }

    //根据id修改账户积分
    @RequestMapping("/updateScoreById")
    public UserVo updateScore(@RequestBody UserVo userVo) {
        return userService.updateScore(userVo);
    }

    //根据id修改生日
    @RequestMapping("/updateBirthById")
    public ResultResponse updateBirthById(@RequestBody BirthdayVo birthdayVo) {
        return userService.updateBirthById(birthdayVo);
    }

    //签到
    @RequestMapping("/signIn")
    public ResultResponse signIn(Integer id) {
        return userService.signIn(id);
    }

    @RequestMapping("find")
    public R find(Integer id){
        User user = userService.find(id);
        return R.success(user);
    }

//    注销
    @RequestMapping("/deleteUser")
    public R deleteUser(Integer id) {
       userService.deleteUser(id);
       return R.success("删除成功");
    }
    //查询我的积分记录页面
    @RequestMapping("/findUserScore")
    public ResultResponse findUserScore(Integer id) {
        return recordService.findUserScore(id);
    }
    //添加积分记录
    @RequestMapping("/addRecord")
    public RecordVo addRecord(@RequestBody RecordVo recordVo){
        return recordService.addRecord(recordVo);
    }


    // 更新用户头像
    @PostMapping("/updateAvatar")
    public R updateAvatar(@RequestBody User user) {
        boolean updated = userService.updateImage(user.getId(), user.getImage());
        if (updated) {
            return R.success("头像更新成功");
        } else {
            return R.error("头像更新失败");
        }
    }


    @RequestMapping("/fileUpload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        System.out.println(inputStream+"===========");
        String s=aliyunOSSUtils.putObject(inputStream,originalFilename);
        return s;
    }
    @RequestMapping("toLoginPassword")
    public ResultResponse toLoginPassword(@RequestBody User user){
        log.info("用户名："+user.getPhone(),"密码："+user.getPassword());
        return userService.toLoginPassword(user);
    }

    //修改密码
    @RequestMapping("updatePassword")
    public ResultResponse updatePassword(@RequestBody User user){
        return userService.updatePassword(user);
    }
}
